//
// Created by 谢哲 on 2021/4/18.
//

#include <iostream>
#include <vector>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int n, m;
    cin >> n >> m;

    vector<vector<int>> matrix(n, vector<int>(n));
    int cnt = 0;
    for (auto &p: matrix) {
        for (auto &q: p)
            q = ++cnt;
    }

    for(;m;--m) {
        int x, y, r, z;
        cin >> x >> y >> r >> z;
        --x, --y;

        if (z==0) {
            for (int i = 1; i <= r; ++i) {
                for (int j = 0; j <= r; ++j) {
                    int a = matrix[x + i][y + j];
                    int b = matrix[x + j][y - i];
                    int c = matrix[x - i][y - j];
                    int d = matrix[x - j][y + i];

                    matrix[x+j][y-i] = a;
                    matrix[x-i][y-j] = b;
                    matrix[x-j][y+i] = c;
                    matrix[x+i][y+j] = d;
                }
            }
        }
        else {
            for (int i = 1; i <= r; ++i) {
                for (int j = 0; j <= r; ++j) {
                    int a = matrix[x + i][y + j];
                    int b = matrix[x + j][y - i];
                    int c = matrix[x - i][y - j];
                    int d = matrix[x - j][y + i];

                    matrix[x+j][y-i] = c;
                    matrix[x-i][y-j] = d;
                    matrix[x-j][y+i] = a;
                    matrix[x+i][y+j] = b;
                }
            }
        }
    }

    for (auto &p: matrix) {
        for (auto &q: p) {
            cout << q << " ";
        }
        cout << "\n";
    }

    return 0;
}